package com.bzxgc.sgwer.common.page;

import com.bzxgc.sgwer.common.util.JsonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class JdbcSqlController {
    private StudentService studentService = new JdbcSqlStudentServiceImpl();

    @RequestMapping("/jdbcsql/list")
    public JsonResult list(HttpServletRequest request) {
        // 接收request里的参数
        String stuName = request.getParameter("stuName"); // 学生姓名

        int gender = Constant.DEFAULT_GENDER;
        String genderStr = request.getParameter("gender");
        if (genderStr != null && !genderStr.equals("")) {
            gender = Integer.parseInt(genderStr); // 获取学生的性别
        }

        // 校验pageNum参数输入合法性
        String pageNumStr = request.getParameter("pageNum");
        if (pageNumStr != null && !StringUtil.isNum(pageNumStr)) {
            return JsonResult.error("参数传输错误");
        }

        int pageNum = Constant.DEFAULT_PAGE_NUM;
        if (pageNumStr != null && !pageNumStr.equals("")) {
            pageNum = Integer.parseInt(pageNumStr); // 显示第几页数据
        }

        int pageSize = Constant.DEFAULT_PAGE_SIZE;
        String pageSizeStr = request.getParameter("pageSize");
        if (pageSizeStr != null && !pageSizeStr.equals("")) {
            pageSize = Integer.parseInt(pageSizeStr); // 每页显示多少条记录
        }

        // 组装查询条件
        Student student = new Student();
        student.setStuName(stuName);
        student.setGender(gender);

        // 调用service获取查询结果
        Pager<Student> result = studentService.findStrudent(student, pageNum, pageSize);

        // 返回结果到页面
        return JsonResult.success(result);
    }
}
